#include "prt_typedef.h"
#include "prt_shell.h"
#include "os_attr_armv8_external.h"
#include "prt_task.h"
#include "prt_sem.h"
#include "prt_task_external.h"

extern U32 PRT_Printf(const char *format, ...);
extern void OsTaskExit(struct TagTskCb *tsk);


OS_SEC_TEXT void MyShellTask(void)
{
    int count = 10;
    PRT_Printf("\r\nI am User Task Created by Shell command!\r\n");
    while (count--);
}

OS_SEC_L4_TEXT U32 ShellCreateTask()
{
    U32 ret;
    TskHandle taskHdl;
    struct TskInitParam taskInitParam = {0};
    char tskName[OS_TSK_NAME_LEN] = "Shell Creat Task";

    /* Create background task. */
    taskInitParam.taskEntry = (TskEntryFunc)MyShellTask;
    taskInitParam.stackSize = 4096;
    // taskInitParam.name = tskName;
    taskInitParam.taskPrio = 63;
    taskInitParam.stackAddr = 0;

    /* 任务调度的必要条件就是有背景任务，此时背景任务还没有创建，因此不会发生任务切换 */
    ret = PRT_TaskCreate(&taskHdl, &taskInitParam);
    if (ret != OS_OK) {
        return ret;
    }
    ret = PRT_TaskResume(taskHdl);
    if (ret != OS_OK) {
        return ret;
    }

    PRT_Printf("\r\nCreate a task Successfully! (优先级:63 PID:%d)", taskHdl);
    return ret;
}

OS_SEC_TEXT void KillTask(U32 task_pid)
{
    struct TagTskCb *taskCb = NULL;
    // 获取 taskPid 对应的任务控制块
    taskCb = GET_TCB_HANDLE(task_pid);
    OsTaskExit(taskCb);
    PRT_Printf("\r\nAlready kill the task by PID:%d", task_pid);
}